package com.neusoft.zqygbc.neusoft_wenda.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.neusoft.zqygbc.neusoft_wenda.R;
import com.neusoft.zqygbc.neusoft_wenda.activity.LoginActivity;
import com.neusoft.zqygbc.neusoft_wenda.activity.MyCollectActivity;
import com.neusoft.zqygbc.neusoft_wenda.activity.MyDraftsActivity;
import com.neusoft.zqygbc.neusoft_wenda.activity.SettingActivity;
import com.neusoft.zqygbc.neusoft_wenda.activity.UserEditActivity;
import com.neusoft.zqygbc.neusoft_wenda.activity.MyFollowActivity;
import com.neusoft.zqygbc.neusoft_wenda.base.RetrofitCallback;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.UserInfo;
import com.neusoft.zqygbc.neusoft_wenda.model.UserModel;
import com.neusoft.zqygbc.neusoft_wenda.view.XCRoundImageView;
import com.squareup.picasso.Picasso;

import retrofit2.Call;

/**
 * Created by Lenovo on 2017/3/13.
 */

public class MoreFragment extends Fragment {
    private Context mContext;
    private TextView btn_login;
    private TextView btn_set;
    private LinearLayout more_sign,more_Nsign,more_user,more_myfollow,more_mycollect,more_mydrafts;
    private XCRoundImageView more_upic;
    private TextView more_username;
    private TextView more_edit;
    private TextView more_Ntext;
    private Button more_login;
    SharedPreferences sp;
    private UserModel userModel;
    private Call call;
    private RetrofitCallback<UserInfo> userInfoRetrofitCallback;
    private UserInfo useredit;

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)

    {
        View view = inflater.inflate(R.layout.tab05, container, false);
        initViews(view);
        initEvents();
        initData();
        return view;

    }

    private void initViews(View view) {
        sp = getActivity().getSharedPreferences("userinfo", mContext.MODE_PRIVATE);
        more_sign = (LinearLayout) view.findViewById(R.id.more_signin);
        more_upic = (XCRoundImageView) view.findViewById(R.id.more_upic);
        more_user = (LinearLayout) view.findViewById(R.id.more_user);
        more_mydrafts = (LinearLayout) view.findViewById(R.id.more_mydrafts);
        more_username = (TextView) view.findViewById(R.id.more_username);
        more_edit = (TextView) view.findViewById(R.id.more_edit);
        btn_set = (TextView) view.findViewById(R.id.more_btn_set);
        more_Nsign = (LinearLayout) view.findViewById(R.id.more_Nsign);
        more_login = (Button) view.findViewById(R.id.more_login);
        more_myfollow = (LinearLayout) view.findViewById(R.id.more_myfollow);
        more_mycollect = (LinearLayout) view.findViewById(R.id.more_mycollect);
        if (sp.getString("user_id", "").equals("")) {
            more_sign.setVisibility(View.GONE);
            more_upic.setVisibility(View.GONE);
            more_user.setVisibility(View.GONE);
            more_username.setVisibility(View.GONE);
            more_edit.setVisibility(View.GONE);
        } else {
            more_Nsign.setVisibility(View.GONE);
            more_login.setVisibility(View.GONE);
        }

    }

    private void initEvents() {
        more_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });
        more_mydrafts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyDraftsActivity.class);
                startActivity(intent);
            }
        });
        btn_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SettingActivity.class);
                startActivity(intent);
            }
        });
        more_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), UserEditActivity.class);
                intent.putExtra("username", useredit.getUsername());
                intent.putExtra("email", useredit.getEmail());
                intent.putExtra("sex", useredit.getSex());
                intent.putExtra("upic", useredit.getUpic());
                intent.putExtra("birth", useredit.getBirth());
                intent.putExtra("signature", useredit.getSignature());
                intent.putExtra("schoolname", useredit.getSchoolname());
                startActivity(intent);
            }
        });
        more_myfollow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyFollowActivity.class);
                intent.putExtra("user_id",sp.getString("user_id", ""));
                startActivity(intent);
            }
        });
        more_mycollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyCollectActivity.class);
                intent.putExtra("user_id",sp.getString("user_id", ""));
                startActivity(intent);
            }
        });

    }

    private void initData() {
        userModel=new UserModel();
        call=userModel.getuserinfo(sp.getString("user_id", ""));
        userInfoRetrofitCallback=new RetrofitCallback<UserInfo>() {
            @Override
            public void onResponse(final UserInfo  userInfo) {
                useredit=userInfo;
                    more_username.setText(userInfo.getUsername());
                    Picasso.with(getContext()).load("http://115.159.93.244/wenda/Public/userphoto/" + userInfo.getUpic()).into(more_upic);
            }
        };
        userModel.bindCallback(call,userInfoRetrofitCallback);


    }


}

